﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using System.Web.Mvc.Html;
using ECube.Web.CMS.Models;
using Oxite.Extensions;
using Oxite.ViewModels;
namespace ECube.Web.CMS.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static void WidgetContainer(this HtmlHelper htmlHelper,String widgetContainerId)
        {
            htmlHelper.WidgetContainer(widgetContainerId, "");
        }
        public static void WidgetContainer(this HtmlHelper htmlHelper, String widgetContainerId, string viewname)
        {
            if (htmlHelper.ViewData.Model is OxiteViewModelItem<Page>)
            {
                OxiteViewModelItem<Page> model = htmlHelper.ViewData.Model as OxiteViewModelItem<Page>;
                if (model != null && model.Item != null)
                {
                    htmlHelper.WidgetContainer(widgetContainerId,viewname,model.Item);
                }
            }       
        }
        public static void WidgetContainer(this HtmlHelper htmlHelper, String widgetContainerId,Page page)
        {
            htmlHelper.WidgetContainer(widgetContainerId, null, page);
        }

        public static void WidgetContainer(this HtmlHelper htmlHelper, String widgetContainerId, string viewname,Page page)
        {
            if (page != null)
            {
                WidgetContainer container = page.Profile.WidgetContainers.Where<WidgetContainer>(wc => wc.Id == widgetContainerId).FirstOrDefault();
                if (container == null)
                {
                    container = new WidgetContainer(widgetContainerId);
                    page.Profile.WidgetContainers.Add(container);
                }
                if (!string.IsNullOrEmpty(viewname))
                {
                    htmlHelper.RenderPartialFromSkin(viewname, container);
                }
                else
                {
                    container.Render(htmlHelper, page.Profile.IsDesign, page.IsEdit);
                }
            }
        }


        public static void Write(this HtmlHelper htmlHelper, String html)
        {
            if (htmlHelper == null || html == null) return;
            htmlHelper.ViewContext.HttpContext.Response.Write(html);
        }

        
        public static void RenderAdminMenu(this HtmlHelper helper, String viewname)
        {
            RenderMenu(helper, viewname, SiteNavigatorService.Create("AdminSiteMap"), "~/Admin");
        }

        public static void RenderMenu(this HtmlHelper helper, String viewname)
        {
            RenderMenu(helper, viewname, SiteNavigatorService.Current, "~/");
        }
        public static void RenderMenu(this HtmlHelper helper, string viewname, SiteNavigatorService service, string startWidthUrl)
        {
            if (string.IsNullOrEmpty(viewname) || service == null) return;

            MvcSiteMapNode node = service.GetNode(startWidthUrl) as MvcSiteMapNode;
            helper.RenderPartialFromSkin(viewname, node);
        }
    }
}
